<!-- 设备用户关联表编辑弹窗 -->
<template>
  <div class="ele-body">
    <a-card :title="deviceCatUserModalApp.title" :bordered="false">
      <a-form
        ref="form2"
        :model="deviceCatUserModalApp.deviceCatUser"
        :rules="deviceCatUserModalApp.rules"
        :label-col="{md: {span: 7}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 17}, sm: {span: 24}}">
        <a-row :gutter="16">

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="设备:" name="deviceId">
              <m-entity-select
                :default-value="deviceCatUserModalApp.deviceCatUser.deviceName"
                v-model:value="deviceCatUserModalApp.deviceCatUser.deviceId"
                placeholder="请选择设备"
                module="repairManagement"
                entity="Device"
                class="ele-fluid"
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="用户:" name="userId">
              <m-entity-select
                :default-value="deviceCatUserModalApp.deviceCatUser.userName"
                v-model:value="deviceCatUserModalApp.deviceCatUser.userId"
                placeholder="请选择用户"
                module="sys"
                show-options-name="nickname"
                :query-filters="{userType:'user'}"
                system="1"
                entity="user"
                class="ele-fluid"
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="管理过期时间:" name="expirationDate">
              <a-date-picker
                v-model:value="deviceCatUserModalApp.deviceCatUser.expirationDate"
                placeholder="请选择管理过期时间"
                value-format="YYYY-MM-DD H:m:s"
                class="ele-fluid"/>
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{md: {offset: 6}}" style="margin-bottom: -20px">
              <!--class="ele-pull-right"-->
              <div v-if="deviceCatUserModalApp.display">
                <teleport to="#deviceCatUserEditModalFooter">
                  <div>
                    <a-space size="middle">
                      <a-button @click="deviceCatUserListApp.editModalShowing=false">取消
                      </a-button>
                      <a-button
                        type="primary"
                        @click="onSubmit(deviceCatUserModalApp.deviceCatUser)"
                        :loading="loading">提交
                      </a-button>
                      <!--<a-button v-if="!eid"
                                type="dashed"
                                @click="continueSubmit"
                                :loading="loading">继续提交
                      </a-button>-->
                    </a-space>
                  </div>
                </teleport>
              </div>
              <div v-if="!deviceCatUserModalApp.display">
                <a-space size="middle">
                  <a-button @click="onBack()">返回列表</a-button>
                  <a-button
                    type="primary"
                    @click="onSubmit(deviceCatUserModalApp.deviceCatUser)"
                    :loading="loading">提交
                  </a-button>
                  <!--<a-button v-if="!eid"
                            type="dashed"
                            @click="continueSubmit"
                            :loading="loading">继续提交
                  </a-button>-->
                </a-space>
              </div>

            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

  </div>

</template>

<script>
import {defineComponent, inject, reactive, onMounted} from 'vue'
import {useRoute, useRouter} from "vue-router"
import {DeviceCatUserService} from "@/views/repair/repairManagement/deviceCatUser/deviceCatUserService";
import {VXETable} from 'vxe-table'
import {useStore} from "vuex";
import regions from 'ele-admin-pro/packages/regions.js';


import MEntitySelect from "@/components/MEntity/entitySelect";

export default defineComponent({
  components: {
    MEntitySelect,
  },
  setup() {
    // 省市区数据
    const cityData = regions
    const route = useRoute();
    const router = useRouter();
    const store = useStore();
    let routeId = route.params.id;
    let deviceCatUserListApp = inject('deviceCatUserListApp', reactive({}));
    let deviceCatUserGrid = inject('deviceCatUserGrid', reactive({}))
    const deviceCatUserModalApp = reactive({
      id: null,
      deviceCatUser: {},
      isEdit: false,
      display: false
    });
    deviceCatUserModalApp.rules = {
      deviceId: [
        {
          required: false,
          type: 'number',
          message: '请输入设备',
          trigger: 'blur'
        }
      ],
      deviceName: [
        {
          required: false,
          type: 'string',
          message: '请输入设备',
          trigger: 'blur'
        }
      ],
      userName: [
        {
          required: false,
          type: 'string',
          message: '请输入用户',
          trigger: 'blur'
        }
      ],
      userId: [
        {
          required: true,
          type: 'number',
          message: '请输入用户',
          trigger: 'blur'
        }
      ],
      expirationDate: [
        {
          required: false,
          type: 'string',
          message: '请输入管理过期时间',
          trigger: 'blur'
        }
      ],
    }

    onMounted(() => {
      //清理数据
      deviceCatUserModalApp.deviceCatUser = reactive({});
      if (routeId) {//路由
        deviceCatUserModalApp.id = routeId;
        deviceCatUserModalApp.display = false;
        findDeviceCatUser(deviceCatUserModalApp.id);
      } else if (deviceCatUserListApp.editModalForEdit) {//修改弹窗
        deviceCatUserModalApp.id = deviceCatUserListApp.currentId;
        deviceCatUserModalApp.display = true;
      } else if (!deviceCatUserListApp.editModalForEdit && deviceCatUserListApp.addModalForEdit) {//新增弹窗
        deviceCatUserModalApp.display = true;
      } else {
        deviceCatUserModalApp.title = "新增";
      }
      if (deviceCatUserModalApp.id) findDeviceCatUser(deviceCatUserModalApp.id);
    })

    const findDeviceCatUser = (id) => {
      DeviceCatUserService.findDeviceCatUserForView(id).then((res) => {
        deviceCatUserModalApp.deviceCatUser = res.data;
      }).catch(error => {
        VXETable.modal.message({content: `查找出错，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:提交 新增&编辑*/
    const onSubmit = (data) => {
      // debugger;
      //当为数组时用逗号连接
      if (data.eid) {
        //修改
        DeviceCatUserService.updateDeviceCatUser(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      } else {
        //新增
        DeviceCatUserService.saveDeviceCatUser(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      }
    }

    /*TODO:继续提交*/
    const continueSubmit = () => {
      VXETable.modal.message({content: 'error 提示', status: 'error'});
    }

    /*TODO:返回列表*/
    const onBack = () => {
      router.push(`/repair/repairManagement/deviceCatUser`);
      store.dispatch('user/tabRemove', route.fullPath);
      setTimeout(() => {
        deviceCatUserGrid.value.commitProxy("query");
      }, 1500);
      deviceCatUserListApp.editModalShowing = false;
    }
    return {
      cityData,
      routeId,
      deviceCatUserListApp,
      deviceCatUserModalApp,
      onSubmit,
      continueSubmit,
      onBack,
    }

  },
})
</script>

<style scoped>

</style>
